package cn.tedu.store.config;

import cn.tedu.store.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 拦截器配置类（指定哪些需要进行拦截，哪些直接放行）
 */
@Configuration //@Configuration注解表示该类是一个配置类
public class LoginInterceptorConfigurer implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //创建登录拦截器对象
        LoginInterceptor loginInterceptor = new LoginInterceptor();

        List<String> patterns = new ArrayList<>();
        patterns.add("/bootstrap3/**");
        patterns.add("/css/**");
        patterns.add("/images/**");
        patterns.add("/js/**");
        patterns.add("/web/register.html");
        patterns.add("/web/login.html");
        patterns.add("/web/index.html");
        patterns.add("/web/product.html");
        patterns.add("/users/reg");
        patterns.add("/users/login");
        patterns.add("/products/**");
        //利用注册工具注册拦截器
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**") //拦截的对象，/**表示拦截所有
                .excludePathPatterns(patterns); //不进行拦截的对象

    }
}
